
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.wxJsPay = wxJsPay;
exports.formatDate = formatDate;
exports.formatEveryDay = formatEveryDay;
exports.transformURL = transformURL;
exports.formatPhoneNumber = formatPhoneNumber;
exports.curEnv = curEnv;
exports.transformQueryString = transformQueryString;
exports.debounce = debounce;
exports.throttle = throttle;
exports.sleep = sleep;
exports.pageData = pageData;
exports.bind = bind;
exports.extend = extend;
exports.merge = merge;
exports.deepMerge = deepMerge;
exports.isEmptyObject = isEmptyObject;
exports.combineURLs = combineURLs;
exports.encode = encode;
exports.isAbsoluteURL = isAbsoluteURL;
exports.Storage = exports.isIdCard = exports.isEmail = exports.isPhoneNumber = exports.rand = exports.prefixZero = void 0;

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function wxJsPay(payParams) {
    var callBack = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    WeixinJSBridge.invoke("getBrandWCPayRequest", payParams, function (_ref) {
        var err_msg = _ref.err_msg;
        return callBack.complete(), err_msg == "get_brand_wcpay_request:ok" ? uni.showToast({
            title: "支付成功",
            complete: callBack.success
        }) : uni.showToast({
            title: "支付失败",
            icon: "none",
            complete: callBack.fail
        });
    });
}

var prefixZero = function prefixZero(n) {
    return String(+n > 9 ? n : "0".concat(n));
};

exports.prefixZero = prefixZero;

function formatDate() {
    var timestamp = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : Date.now();
    var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "Y-M-D h:m:s";

    /**
     * @method prefixZero
     * @param {timestamp} number
     * @param {format} string
     * @return {string}
     */
    var date = new Date(timestamp);
    var time = {
        Y: date.getFullYear(),
        M: date.getMonth() + 1,
        D: date.getDate(),
        W: date.getDay(),
        h: date.getHours(),
        m: date.getMinutes(),
        s: date.getSeconds()
    };
    return format.replace("Y", String(time.Y)).replace("M", prefixZero(time.M)).replace("D", prefixZero(time.D)).replace("W", String(time.W)).replace("h", prefixZero(time.h)).replace("m", prefixZero(time.m)).replace("s", prefixZero(time.s));
}
/**
* @param { Date } start
* @param { Date } end
* @returns { Array<string> }
*/


function formatEveryDay(start, end) {
    var format = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
    var dateList = [];

    while (end.getTime() - start.getTime() >= 0) {
        dateList.push(formatDate(start.getTime(), format));
        start.setDate(start.getDate() + 1);
    }

    return dateList;
}

function transformURL(url, params, hash) {
    var paramsArr = Object.keys(params).map(function (key) {
        return "".concat(key, "=").concat(params[key]);
    });
    return encodeURIComponent("".concat(url, "?").concat(paramsArr.join("&")).concat(hash ? "#" + hash : ""));
}

function formatPhoneNumber(phoneNumber) {
    return phoneNumber.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
}

var rand = function rand(min, max) {
    return ~~(Math.random() * (max - min + 1) + min);
};

exports.rand = rand;

function curEnv() {
    var curEnv;
    return curEnv;
}

function transformQueryString(params) {
    if (typeof params === "string") {
        var queryStrings = decodeURIComponent(params).match(/[^?&]+=[^?&]+/g);
        return queryStrings ? Object.fromEntries(queryStrings.map(function (item) {
            return item.split(/^([^=]*)=*/).filter(function (item) {
                return item;
            });
        })) : Object();
    }

    if (_typeof(params) === "object") {
        return Object.keys(params).map(function (key) {
            return "".concat(key, "=").concat(params[key]);
        }).join("&");
    }

    throw Error("Parameter error");
}

var isPhoneNumber = function isPhoneNumber(str) {
    return /^1[0-9]{10}$/.test(str);
};

exports.isPhoneNumber = isPhoneNumber;

var isEmail = function isEmail(str) {
    return /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(str);
};

exports.isEmail = isEmail;

var isIdCard = function isIdCard(str) {
    return /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/.test(str);
}; // 防抖  


exports.isIdCard = isIdCard;

function debounce(fn) {
    var _arguments = arguments;
    var wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 500;
    var isImmediate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
    var timerId = 0,
        flag = true;
    return function () {
        timerId && clearTimeout(timerId);

        if (flag) {
            isImmediate && fn.apply(fn, _arguments);
            flag = false;
        }

        timerId = setTimeout(function () {
            isImmediate ? flag = true : fn.apply(fn, _arguments);
        }, wait);
    };
} // 节流  


function throttle(fn) {
    var _arguments2 = arguments;
    var wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 500;
    var isImmediate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
    var flag = true;
    return function () {
        if (flag) {
            isImmediate && fn.apply(fn, _arguments2);
            flag = false;
            setTimeout(function () {
                !isImmediate && fn.apply(fn, _arguments2);
                flag = true;
            }, wait);
        }
    };
}

function sleep(second) {
    return new Promise(function (resolve) {
        return setTimeout(resolve, second * 1000);
    });
}

function pageData() {
    var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : -2;
    var pages = getCurrentPages();
    return pages[pages.length + page];
}
/**
 * 缓存 增删查改
 */


var Storage = {
    set: function set(key, value) {
        var time = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
        var timestamp = Date.now();
        uni.setStorageSync(key, {
            value: value,
            createTime: timestamp,
            expireTime: time ? timestamp + time * 1000 : time
        });
        return value;
    },
    remove: function remove() {
        for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
            args[_key] = arguments[_key];
        }

        args.forEach(function (key) {
            return uni.removeStorageSync(key);
        });
    },
    get: function get(key) {
        var _ref2 = [Date.now(), uni.getStorageSync(key)],
            timestamp = _ref2[0],
            _ref2$ = _ref2[1],
            expireTime = _ref2$.expireTime,
            value = _ref2$.value;
        return expireTime && timestamp >= expireTime ? this.remove(key) : value;
    },
    update: function update(key, prototypes) {
        var expireTime = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;

        var _ref3 = uni.getStorageSync(key) || {},
            value = _ref3.value,
            createTime = _ref3.createTime,
            storageExpire = _ref3.expireTime;

        if (Object.prototype.toString.call(value) === "[object Object]") {
            uni.setStorageSync(key, {
                value: Object.assign(value, prototypes),
                createTime: createTime,
                expireTime: (expireTime ? Date.now() + expireTime * 1000 : expireTime) || storageExpire
            });
            return value;
        }

        return this.set(key, prototypes, expireTime);
    },
    clear: function clear() {
        uni.clearStorageSync();
    }
};
exports.Storage = Storage;

function bind(fn, thisArg) {
    return function warp() {
        return fn.apply(thisArg, Array.from(arguments));
    };
}

function extend(a, b, thisArg) {
    var o = Object.getOwnPropertyNames(b);
    o.forEach(function (attr) {
        if (thisArg && typeof b[attr] === "function") {
            a[attr] = bind(b[attr], thisArg);
        } else {
            a[attr] = b[attr];
        }
    });
    return a;
}

function merge() {
    var result = {};

    for (var _len2 = arguments.length, arg = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
        arg[_key2] = arguments[_key2];
    }

    arg.forEach(function (e) {
        for (var key in e) {
            if (_typeof(e[key]) === "object" && !isEmptyObject(e[key])) {
                merge(result[key], e[key]);
            }

            result[key] = e[key];
        }
    });
    return result;
}

function deepMerge() {
    var result = {};

    for (var _len3 = arguments.length, arg = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
        arg[_key3] = arguments[_key3];
    }

    arg.forEach(function (e) {
        if (e && _typeof(e) === "object" && !isEmptyObject(e)) {
            Object.keys(e).forEach(function (key) {
                if (_typeof(e[key]) === "object") {
                    return result[key] = deepMerge(result[key], e[key]);
                }

                result[key] = e[key];
            });
        }
    });
    return result;
}

function isEmptyObject(obj) {
    return obj === null || Object.getOwnPropertyNames(obj).length === 0;
}

function combineURLs(baseURL, relativeURL) {
    return relativeURL ? baseURL.replace(/\/+$/, "") + "/" + relativeURL.replace(/^\/+/, "") : baseURL;
}

function encode(val) {
    return encodeURIComponent(val).replace(/%40/gi, "@").replace(/%3A/gi, ":").replace(/%24/g, "$").replace(/%2C/gi, ",").replace(/%20/g, "+").replace(/%5B/gi, "[").replace(/%5D/gi, "]");
}
/**
 * Determines whether the specified URL is absolute
 *
 * @param {string} url The URL to test
 * @returns {boolean} True if the specified URL is absolute, otherwise false
 */


function isAbsoluteURL(url) {
    // A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
    // RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
    // by any combination of letters, digits, plus, period, or hyphen.
    return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url);
}
